var JavaBase = Class.create();
/**
  Object that detects current browser
**/
function JavaBase_Browser () 
{
  d=document;
  this.agt=navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);
  this.dom=(d.getElementById)?1:0; // true for ie6, ns6
  this.ns=(d.layers);
  this.ns4up=(this.ns && this.major >=4);
  this.ns6=(this.dom&&navigator.appName=="Netscape");
  this.op=(window.opera? 1:0);
  this.ie=(d.all);
  this.ie4=(d.all&&!this.dom)?1:0;
  this.ie4up=(this.ie && this.major >= 4);
  this.ie5=(d.all&&this.dom);
  this.win=((this.agt.indexOf("win")!=-1) || (this.agt.indexOf("16bit")!=-1));
  this.mac=(this.agt.indexOf("mac")!=-1);
}

javaBaseBrowser = new JavaBase_Browser();

if (webPageMode == undefined) 
{
   var webPageMode=(document.all && !window.opera && document.compatMode && document.compatMode!="BackCompat")? 1 : 0 
}

/**
 * Dom utility. Should be replaced by standard library available.
**/
JavaBase.DomUtil =
{
  removeChildrenFromNode : function(node)
  {
    if(node !== undefined && node !== null)
    {
      return;
    }
   
    var len = node.childNodes.length;
   
    while (node.hasChildNodes())
    {
      this.removeChildrenFromNode(node.firstChild);
      node.removeChild(node.firstChild);
    }
  },

  setAttributeValue : function(ele, name, value)
  {
    var element = $(ele);
    if(element)
      element.setAttribute(name, value);    
  },
  //return attribute value for an element
  getAttributeValue : function(ele, name)
  {
    var element = $(ele);
    if(element && element.attributes && element.attributes[name])
      return element.attributes[name].value;
    return null;  
  },
  changeParent : function(ele, newParent)
  {
     var element = $(ele);
     if(newParent&&element.parentNode!= newParent)
     {
        if(element.parentNode)
            element.parentNode.removeChild(element);
        //newParent.appendChild(element);
     }  
  },
    
  getParentNode : function(ele)
  {
    return $(ele).parentNode;
  }
}

/**
   Location and dimension functions
**/
JavaBase.Dimension =
{
     cachedDivs : null,
    //
    getScrollLeft : function (ele) {
       var scrOfX = 0;
       
      node = ele;
      while(node && node.parentNode )
      {
        if(node.parentNode.scrollLeft)
        {
          scrOfX += node.parentNode.scrollLeft;
        }
        node = node.parentNode;
      }
      return scrOfX;
    },

    getScrollTop : function (ele) {
      var scrOfY = 0;

      node = ele;
      while(node && node.parentNode )//&& node.parentNode!= document.Body)
      {
        if(node.parentNode.scrollTop)
        {
          scrOfY += node.parentNode.scrollTop;
        }
        node = node.parentNode;
      }
      
      return scrOfY ;
    },

    getClientHeight : function () {
      switch (webPageMode) {
        case 0:
          return document.body.clientHeight;
        case 1:
          if (javaBaseBrowser.ie) {
            return document.documentElement.clientHeight;
          } else {
            return document.body.clientHeight;
          };
          break;
        case 2:
          return document.body.clientHeight;
      }
    },

    getClientWidth :function () 
    {
      switch (webPageMode) {
        case 0:
          return document.body.clientWidth;
        case 1:
          if(javaBaseBrowser.dom)
            return document.body.clientWidth;
            
          if (javaBaseBrowser.ie) {
            return document.documentElement.clientWidth;
          } else {
            return document.body.clientWidth;
          }
          break;
        case 2:
          return document.body.clientWidth;
      }
    },   
    
  getElementsByPosition : function (pt)
    {
      var ele = [];    
      if(this.cachedDivs==null)
      {
        this.cachedDivs = document.getElementsByTagName('*');
       // this.cachedDivs.reverse();
      }
      
      var divs = this.cachedDivs;

      //window.status=divs.length;
      for (var i = 0; i < divs.length; i++) {
        if(divs[i].id) // we only interested in ones that has an id
        {
          rect = JavaBase.Dimension.getElementRect(divs[i]);  
          
          if(JavaBase.Dimension.isInRect(rect, pt))
          {
            ele[ele.length] = divs[i];
          }
        }
      }
      
      return ele.reverse();
    },
  getInnerElementRect : function (elem) 
  {
    if(elem==null)
    return {left:0, top:0,width:0, height:0};
    var ele =$(elem);
    var rect = this.getElementRect(elem);
    
    if(ele.style.left)
      rect.left = parseInt(ele.style.left);
    if(ele.style.top)
      rect.top = parseInt(ele.style.top);

    if(ele.style.width)
      rect.width = parseInt(ele.style.width);
    if(ele.style.height)
      rect.height = parseInt(ele.style.height);
      
    with(rect)
    { 
        left = left + parseInt(ele.style.borderLeftWidth?ele.style.borderLeftWidth:0);
        top = top + parseInt(ele.style.borderTopWidth?ele.style.borderTopWidth:0);
    }      
    
    if(javaBaseBrowser.ie)
    {
      with(rect)
      { 
        width = width 
            - parseInt(ele.style.borderLeftWidth?ele.style.borderLeftWidth:0)
            - parseInt(ele.style.borderRightWidth?ele.style.borderRightWidth:0);
        height= height 
            - parseInt(ele.style.borderTopWidth?ele.style.borderTopWidth:0)
            - parseInt(ele.style.borderBottomWidth?ele.style.borderBottomWidth:0);
      }
    }
      
    return rect;
  },

//test if a point is in a rect
//rect {left, top, width, height}
//pt {left, top}
  isInRect: function (rect, pt)
    {
      try
      {
        result = 
          (pt.left > rect.left) && (pt.left < (rect.left+ rect.width))
          &&
          (pt.top > rect.top) && (pt.top < (rect.top+rect.height))
          ;
        return result;
      }catch(e)
      {
        return false;
      }
    },

/**
 * Returns element top left position, absolute as a recode (left, topo) for
 * a give id
 
 * in variable could be either the element or its id
 */
  getElementRect : function (elem, includeMargin) 
  {
      var ele = $(elem);
      if(!ele)
      {
        return {left:0, top:0, width:1000, height:1000};
      }
      
      var width = JavaBase.Dimension.getElementWidth(ele);
      var height = JavaBase.Dimension.getElementHeight(ele);
     
      pt = Position.cumulativeOffset(ele);
      
      var borderWidth = (ele.style.borderLeftWidth ?  parseInt(ele.style.borderLeftWidth):0)
      +(ele.style.borderRightWidth ?  parseInt(ele.style.borderRightWidth):0);
      var paddingWidth = (ele.style.paddingLeft ?  parseInt(ele.style.paddingLeft):0)
      + (ele.style.paddingRight ?  parseInt(ele.style.paddingRight):0);
      
      var borderHeight = (ele.style.borderTopWidth ?  parseInt(ele.style.borderTopWidth):0)
      +(ele.style.borderBottomWidth ?  parseInt(ele.style.borderBottomWidth):0);
      var paddingHeight = (ele.style.paddingTop ?  parseInt(ele.style.paddingTop):0)+
      (ele.style.paddingBottom ?  parseInt(ele.style.paddingBottom):0);
      
      var marginTop = ele.style.marginTop ?  parseInt(ele.style.marginTop):0;
      var marginLeft = ele.style.marginLeft ?  parseInt(ele.style.marginLeft):0;
      
      weight = includeMargin?0:1;
      width = width-weight*(borderWidth+paddingWidth);
      height = height-weight*(borderHeight+paddingHeight);
      return {left:pt[0] - weight*marginLeft , top:pt[1]- weight*marginTop, width:width, height: height};
  },

  getWindowRect : function()
   {
     var winWidth = 0, winHeight = 0;

     if( typeof( window.innerWidth ) == 'number' ) {
       //Non-IE
       winWidth = window.innerWidth;
       winHeight = window.innerHeight;
     } else if( document.documentElement && (document.documentElement.clientWidth || 
            document.documentElement.clientHeight ) ) {
       //IE 6+ in 'standards compliant mode'
       winWidth = document.documentElement.clientWidth;
       winHeight = document.documentElement.clientHeight;
     } else if( document.body && ( document.body.clientWidth ||
          document.body.clientHeight ) ) {
       //IE 4 compatible
       winWidth = document.body.clientWidth;
       winHeight = document.body.clientHeight;
     }
     
     return {left : 0, top:0, width:winWidth, height:winHeight};
   },

//get width in px
  getElementWidth : function (elem) {
    return Element.getDimensions(elem).width;
  },
  
  getElementHeight : function (elem) {
    return Element.getDimensions(elem).height;
  },

  getElementPosition : function (elt) {
    var rect = this.getElementRect(elt);
    return {left:rect.left, top:rect.top};
  }
}
/**
 * Events handling class
**/
JavaBase.EventFactory =
{
  KEY_BACKSPACE: 8,
  KEY_TAB:       9,
  KEY_RETURN:   13,
  KEY_ESC:      27,
  KEY_LEFT:     37,
  KEY_UP:       38,
  KEY_RIGHT:    39,
  KEY_DOWN:     40,
  KEY_DELETE:   46,

  BUTTON_LEFT:  1,
  BUTTON_RIGHT: 3,
  BUTTON_MIDDLE: 2,
  
  getEventSource :  function (e)
  {
    var source = null;
    if(window.event)
    {
      var evt = window.event;
      source = evt.target?evt.target:evt.srcElement;
    }else
      source = e.target;
    return source;
  },
  
  getButton : function (e)
  {
    if(window.event)
    {
       if(window.event.button==2)
        return this.BUTTON_RIGHT;
      else
        return this.BUTTON_LEFT;
    }else
      return e.which;
  },

  cancelBubbleFor : function(event)
  {
  
    if (event.preventDefault) {
   //   event.preventDefault();
      event.stopPropagation();
    } else {
   //   event.returnValue = false;
      event.cancelBubble = true;
    }
   // Event.stop(event);
  },
  getPosition :  function (e)
  {
    var evt = e;
    if(window.event)
    {
      evt = window.event;
    }
    if(evt)
      return {left:evt.clientX, top:evt.clientY};
    else
      return {left:0, top:0};
  },
  
  getKey : function (e)
  {
    var evt = e;
    if(window.event)
    {
      evt = window.event;
    }
    return evt.keyCode;
  },
  isReturnKey: function(e) {
   // alert(this.KEY_RETURN+' ' + this.getKey(e));
      return this.KEY_RETURN==this.getKey(e);
  },
  getChar : function (e)
  {
    var evt = e;
    if(window.event)
    {
      evt = window.event;
    }
    
    var key=(evt.charCode)?evt.charCode:
            ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));

    return key;
  },
  
  //modified from effects2.js
  addEventListener: function(element, name, listener) {
    if($(element).addEventListener) $(element).addEventListener(name,listener,false)
      else if($(element).attachEvent) $(element).attachEvent("on" + name,listener);
  },

  registerKeypressListener: function(element, listener) {             /* special handling needed */
    if(navigator.appVersion.indexOf('AppleWebKit')>0)
      { $(element).addEventListener("keydown",listener,false); return; }
    if($(element).addEventListener) $(element).addEventListener("keypress",listener,false)
      else if($(element).attachEvent) $(element).attachEvent("onkeydown",listener);
  },

  registerBlurListener: function(element, listener) {
    this.addEventListener(element, "blur", listener);
  },

  registerClickListener: function(element, listener) {
    this.addEventListener(element, "click", listener);
  },

  registerWindowResizeListener: function(listener) {
    this.addEventListener(window, "resize", listener);
  },
  registerScrollListener: function(listener) {
    this.addEventListener(window, "scroll", listener);
  },

  registerWindowLoadListener: function(listener) {
    this.addEventListener(window, "load", listener);
  },
  registerWindowUnLoadListener: function(listener) {
    this.addEventListener(window, "unload", listener);
  },

  registerHoverListener: function(element, listener) {
    this.addEventListener(element, "mouseover", listener);
  },

  registerMousedownListener: function(element, listener) {
    this.addEventListener(element, "mousedown", listener);
  },

  registerMouseupListener: function(element, listener) {
    this.addEventListener(element, "mouseup", listener);
  },

  registerMousemoveListener: function(element, listener) {
    this.addEventListener(element, "mousemove", listener);
  }
  
};

/**
  Window utility
**/
var _modelDialogBox=null;

JBUtil = 
{
    hideGT : function()
    {
        if($('_glasstop'))
        {
            $('_glasstop').style.display='none';
            document.body.removeChild($('_glasstop'));
        }
    },
    showGT : function (gtColor)
    {
        if($('_glasstop'))
            document.body.removeChild($('_glasstop'));
    
        var bodyRect = JavaBase.Dimension.getWindowRect();
        var glass = document.createElement("div");
        glass.id='_glasstop';
        with(glass.style)
        {
            left = bodyRect.left + 'px';
            top = JavaBase.Dimension.getScrollTop()+'px';
            height = bodyRect.height + 'px';
            width = bodyRect.width + 'px';
            display="block";
            position="absolute";
            backgroundColor=gtColor?gtColor:"#eee";
            zIndex = 9;
        }
    
        with(glass.style)
        {
            filter="alpha(opacity=80)";
            opacity=0.8;      
        }
        document.body.appendChild(glass);
    },
    showDialogBox : function(panelName, modal, color, effect)
    {
      try
        {
          if(_modelDialogBox!=null)
          {
            _modelDialogBox=null;
          }
          ele = $(panelName);
         
          if(ele)
          {
            _modelDialogBox = ele;
            panelWidth = ele.style.width?parseInt(ele.style.width):500;
            panelHeight = ele.style.height?parseInt(ele.style.height):250;
            
            rect = JavaBase.Dimension.getElementRect(panelName);
            
            topPos = (document.documentElement.scrollTop || document.body.scrollTop) + 80;
            leftPos= (JavaBase.Dimension.getWindowRect().width - panelWidth)/2;
            ele.style.top= topPos +'px';
            ele.style.left=leftPos+'px';
            ele.style.zIndex=100;
            if(modal) JBUtil.showGT(color);
          }
          
          Effect.Appear(ele);
          
        }catch (e)
        {
        
        }
    },    
    placeScreenCenter : function(panelName, effect)
    {
      try
        {
          ele = $(panelName);
          if(ele)
          {
            panelWidth = ele.style.width?parseInt(ele.style.width):500;
            panelHeight = ele.style.height?parseInt(ele.style.height):250;
            
            rect = JavaBase.Dimension.getElementRect(panelName);
            
            topPos = (document.documentElement.scrollTop || document.body.scrollTop) + 
            (JavaBase.Dimension.getWindowRect().height - panelHeight)*.34
            leftPos= (JavaBase.Dimension.getWindowRect().width - panelWidth)/2;
            ele.style.top= topPos +'px'
            ele.style.left=leftPos+'px';
            //show the element
            ele.style.display="";
          }
        }catch (e)
        {
        }
    },
    
    openWindow :function(url) {
       window.open(url);   
    },
    
    getUrl :function()
    {
      var url = window.location.href;
      var firstLoc = url.indexOf('?');
      if(firstLoc != -1)
        url = url.substring(0, firstLoc);
      return url;
    },
    /*Open a new url*/
    gotoUrl : function(dest)
    {
      window.location.href =dest;
    },
    
    /*Reload current url*/
    reload :function()
    { 
      pos = this.getScrollXY();
      this.gotoUrl(window.location.href);
      this.setScrollXY(pos);
    },

    getScrollXY : function () {
        var scrOfX = 0, scrOfY = 0;
      if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
      } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
      }
      return {left : scrOfX, top : scrOfY};
    },
    
    setScrollXY : function (pos) {
      window.scrollTo(pos.left, pos.top);
        /*
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        //alert(pos.top);
        document.body.scrollTop = pos.top;
        document.body.scrollLeft = pos.left;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        document.documentElement.scrollTop= pos.top;
        document.documentElement.scrollLeft= pos.left;
      }else if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        window.pageYOffset = pos.top;
        window.pageXOffset = pos.left;
      }*/
    }
}; 

/*in line editor */
var jbEditor =
{
  editerFormSrc :
      '<span width="100%" id="jbInlineEditorHolder">'+
      '<FORM name="jbInlineEditorForm" id="jbInlineEditorForm" style="display:inline;" method="post">'+
              '<span id="editingField" >'+
                '<INPUT style="border:0px; background-color:silver;" name="jbInlineEditFieldName" type="text" id="jbInlineEditFieldName"/>'+
              '</span>'+
          '<INPUT name="jbInlineEditorFieldSrc" type="hidden" id="jbInlineEditorFieldSrc"/>'+
      '</FORM>' +
      '</span>' +
      '',

  editAreaFormSrc:
      '<span width="100%" id="jbInlineEditorHolder"><TABLE width="100%" cellSpacing="0" cellPadding="0"  >'+
          '<TR >'+
            '<TD colspan="2" align="Left" valign="Top" style="text-align:Left;" >'+
              '<span id="editingField">'+
                '<TEXTAREA name="jbInlineEditFieldName" style="font-size:12px;font-family: arial, sans-serif;background-color:white;border-width:0px;" rows="4" id="jbInlineEditFieldName"></TEXTAREA>'+
                '<INPUT name="jbInlineEditorFieldSrc" type="hidden" id="jbInlineEditorFieldSrc"/>'+
              '</span>'+
            '</TD>'+
          '</TR>'+
          '<TR >'+
            '<TD colspan="2" style="vertical-align:top; text-align:Left;" >'+
              '<input name="jbInlineEditorSubmit" type="button" id="jbInlineEditorSubmit" value="Submit" />'+
              '<input name="jbInlineEditorCancel" type="button" id="jbInlineEditorCancel" value="Cancel" />'+
            '</TD>'+
          '</TR>' +
      '</TABLE></span>',
      
    currentField :null,
    finishEditing :function (newValue)
    {
        
      var urlStr = $('jbInlineEditorHolder').url;
      $('jbInlineEditorHolder').style.display='none';
      var src = $($('jbInlineEditorFieldSrc').value);
      src.innerHTML=newValue;
      src.onclick=jbEditor.currentField.events.onclick;
      src.onmouseover = jbEditor.currentField.events.onmouseover;
      src.onmouseout = jbEditor.currentField.events.onmouseout;
     
      this.currentField=null;
    },

    htmlArea : null,
    
    cancel :function (e)
    {
      
      $('jbInlineEditorHolder').style.display='none';
      var src = $($('jbInlineEditorFieldSrc').value);
      src.innerHTML=jbEditor.currentField.org;
      src.onclick=jbEditor.currentField.events.onclick;
      src.onmouseover = jbEditor.currentField.events.onmouseover;
      src.onmouseout = jbEditor.currentField.events.onmouseout;
      this.currentField=null;
      return false
    },
    
    inlineEditArea : function (ele2Edit, url, widthStr, heightStr, rich)
    {
      if(!ele2Edit)
        return;
      
      var id = ele2Edit.id;
     
      url = url+"?editAction=change&jbCompId="+ele2Edit.id;
      
      if(jbEditor.currentField)
      {
        jbEditor.currentField.cancel();
      }
      
      var org = ele2Edit.innerHTML;
     
      var styleText = ele2Edit.style.cssText;
      
      jbEditor.currentField = new jbEditor.EditingField(id, org, url);
      jbEditor.currentField.events={
        onmouseover:ele2Edit.onmouseover,
        onmouseout:ele2Edit.onmouseout,
        onclick:ele2Edit.onclick
      };
      
      ele2Edit.innerHTML = jbEditor.editAreaFormSrc;
      
      ele2Edit.onmouseover='';
      ele2Edit.onclick='';
      ele2Edit.onmouseout='';
      
      var editField = $('jbInlineEditFieldName');
      
      with ($('jbInlineEditFieldName').style)
      {
        backgroundColor=ele2Edit.style.backgroundColor;
        borderWidth="0px";
        borderColor="silver";
        fontSize=ele2Edit.style.fontSize;
        color = ele2Edit.style.color;
        fontFamily=ele2Edit.style.fontFamily;
      }      
     
      if(!rich)
        org = this.br2cr(org);
       
      editField.value = org;
      
      $('jbInlineEditorHolder').url = url;
    
      editField.className = ele2Edit.className;
    
      $('jbInlineEditorFieldSrc').value = id;
      
      $('jbInlineEditorSubmit').onclick = function ()
      {
          var newValue = $('jbInlineEditFieldName').value;
          
          var urlStr = $('jbInlineEditorHolder').url;
          if(jbEditor.htmlArea)
            newValue = jbEditor.htmlArea.getHTML();
          try
          {
            JB.Ajax.post(urlStr, newValue, function (){jbEditor.finishEditing(newValue);});
          }catch(e)
          {
             
            //
          }
          jbEditor.htmlArea=null;
          return false;
      };

      $('jbInlineEditorHolder').style.display='inline';
      $('jbInlineEditorCancel').onclick=jbEditor.cancel;
      editField.style.width=widthStr;

      editField.style.height=heightStr?heightStr:'200px';
      editField.focus();
      editField.select();
      
      if(rich)
      {
        jbEditor.htmlArea = new HTMLArea(editField);
        jbEditor.htmlArea.generate();
      }
      return editField;
    },

    inlineEditField : function (ele2Edit, url, widthStr)
    {
      if(ele2Edit)
      {
        if(jbEditor.currentField)
        {
          jbEditor.currentField.cancel();
        }

        var id = ele2Edit.id;
        var org = ele2Edit.innerHTML;
        var styleText = ele2Edit.style.cssText
    
        jbEditor.currentField = new jbEditor.EditingField(id, org, url);
        jbEditor.currentField.events={
          onmouseover:ele2Edit.onmouseover,
          onmouseout:ele2Edit.onmouseout,
          onclick:ele2Edit.onclick
        };

        ele2Edit.onmouseover='';
        ele2Edit.onmouseout='';
        ele2Edit.onclick='';
        ele2Edit.innerHTML = jbEditor.editerFormSrc;
        
        var editField = $('jbInlineEditFieldName');

          editField.className=ele2Edit.className;
        with (editField.style)
        {
          backgroundColor=ele2Edit.style.backgroundColor;
          borderWidth="0px";
          fontSize=ele2Edit.style.fontSize;
          color = ele2Edit.style.color;
          fontFamily=ele2Edit.style.fontFamily;
          fontWeight=ele2Edit.style.fontWeight;
        }          
        editField.className=editField.className;
        editField.value = org;
  
        url = url+"?editAction=change&jbCompId="+ele2Edit.id;
        
        $('jbInlineEditorHolder').url = url;
    
        $('jbInlineEditorFieldSrc').value = id;
        $('jbInlineEditorForm').onsubmit = function ()
          {
           
            var newValue = $('jbInlineEditFieldName').value;
            
            var urlStr = $('jbInlineEditorHolder').url;

            try
            {
              
              JB.Ajax.post(urlStr, newValue, function (){jbEditor.finishEditing(newValue);});
            }catch(e)
            {
              //
            }
            return false
          };
          
        $('jbInlineEditorHolder').style.display='inline';
        editField.style.width=widthStr;
        editField.focus();
        editField.select();
      }
    },
    
    br2cr : function  (org)
    {
      newStr = org.replace(/<br>|<BR>/,"\n");
      if(!newStr.match(/<br>|<BR>/))
        return newStr;
      return this.br2cr(newStr);
    }    
}

jbEditor.EditingField = function (id, org, url)
{
  this.id=id;
  this.org=org;
  this.url=url;
  this.events=null;
  this.cancel=function ()
    {
      try
      {
        $('jbInlineEditorHolder').style.display='none';
        var org =$(this.id);
        org.innerHTML=this.org;
        org.onmouseover = this.events.onmouseover;
        org.onmouseout = this.events.onmouseout;
        org.onclick = this.events.onclick;
      }catch( e)
      {
      //in case of except, does nothing
      }
    };
}

function inlineRichEditor(ele2Edit, url, width, height)
{
      dim = JavaBase.Dimension.getInnerElementRect(ele2Edit);
      if(width)
        dim.width = width;
      if(height)
        dim.height=height;
   jbEditor.inlineEditArea($(ele2Edit), url, dim.width+'px', dim.height+'px', true);
}

function inlineEditField(ele2Edit, url, widthStr, heightStr)
{
   jbEditor.inlineEditField(ele2Edit, url, widthStr, heightStr);
}

function inlineE(e, url, tags, rich, width, height)
{
    ele2Edit = JavaBase.EventFactory.getEventSource(e);
      
    if(tags && tags.indexOf(ele2Edit.tagName)!=-1)
    {
      dim = JavaBase.Dimension.getInnerElementRect(ele2Edit);
      if(width)
        dim.width = width;
      if(height)
        dim.height=height;
      if(dim.height>40||rich)
        jbEditor.inlineEditArea(ele2Edit, url, (dim.width - 2)+'px', (dim.height+100)+'px',rich);
      else
        jbEditor.inlineEditField(ele2Edit, url, (dim.width - 2)+'px', dim.height+'px');
        
      inlineEditor=ele2Edit;
      $('jbInlineEditFieldName').focus();
      return false;
    }
}

function inlineEditArea (ele2Edit, url)
{
   rect = JavaBase.Dimension.getElementRect(ele2Edit);
   widthStr = rect.width+'px';
   heightStr = rect.height+'px';
   jbEditor.inlineEditArea($(ele2Edit), url, widthStr, heightStr);
}

/*Default layout manager*/
var LayoutManager =
{
  listeners : [],
  addListener:function (payload, func)
  { 
    hasPayload = null;
    for(i=0;i<this.listeners.length;i++) {
        if(payload==this.listeners[i].payload)
            return;
    }
    this.listeners[this.listeners.length] = {payload:payload, func:func};
  },
  
  removeListener:function (payload)
  {
    temp = [];
    for(i=0;i<this.listeners.length;i++)
    {
        if(payload != this.listeners[i].payload)
        {
            temp[temp.length] = this.listeners[i];
        }
    }
    this.listeners = temp;
  },
  
  onLayout : function(e)
  {    
    var windowRect = JavaBase.Dimension.getWindowRect();
    if($('_glasstop')!=null)
    {
      with($('_glasstop').style)
      {
        width = (windowRect.width -2)+'px';
        height = (windowRect.height -2)+'px';
      }
    }

    if(_modelDialogBox!=null)
    {
      JBUtil.placeScreenCenter(_modelDialogBox.id);
    }

    if(LayoutManager.listeners)
    {
        for(i=0;i<LayoutManager.listeners.length;i++)
        {
            listener = LayoutManager.listeners[i];
            listener.func(listener.payload);
        }
    }
    if($('_layoutleft')==null)
      return;
 
 
    if($('_layoutright')==null)
      return;

    windowRect = JavaBase.Dimension.getWindowRect();
    
    var topRect = {top:0,left:0,width:0,height:0};
    if($('_layouttop')!=null)
      topRect = JavaBase.Dimension.getElementRect('_layouttop');

    var top1Rect = {top:0,left:0,width:0,height:0};
    if($('_layouttop1')!=null)
      top1Rect = JavaBase.Dimension.getElementRect('_layouttop1');
      
    var leftRect = JavaBase.Dimension.getElementRect('_layoutleft');
    var rightRect = JavaBase.Dimension.getElementRect('_layoutright');

    with($('_layoutright').style)
    {
      height = (windowRect.height - topRect.height - top1Rect.height)+'px';
      if($('_layoutleft').style.width.indexOf('px')!=-1)
      {
        width = (windowRect.width - leftRect.width-30)+'px';
      }
    }
    
    with($('_layoutleft').style)
    {
      height = (windowRect.height - topRect.height - top1Rect.height)+'px';
    }

    if($('_layoutmiddle')!=null)
    with($('_layoutmiddle').style)
    {
      height = (windowRect.height - topRect.height - top1Rect.height)+'px';
    }
  }
}

JavaBase.EventFactory.registerWindowResizeListener(LayoutManager.onLayout);
JavaBase.EventFactory.registerWindowLoadListener(LayoutManager.onLayout);

/*Timer*/
function JBTimerManager()
{
  this.timers = new Array();
  this.add = function (id, func2Run, interval, numberOfTimes)
  {
    var timer =new Timer(id,func2Run,interval,numberOfTimes);
    this.timers[this.timers.length]=timer;
    return timer;
  };

  this.get = function (timerId)
  {
    for(i=0;i<this.timers.length;i++)
    {
      if(this.timers[i].id==timerId)
        return this.timers[i];
    }
  };
  this.getTime = function()
  {
    var curDateTime = new Date()
    var curHour = curDateTime.getHours()
    var curMin = curDateTime.getMinutes()
    var curSec = curDateTime.getSeconds()
    var curAMPM = " AM"
    var curTime = ""
    if (curHour >= 12){
        curHour -= 12
        curAMPM = " PM"
    }
    if (curHour == 0) curHour = 12
    curTime = curHour + ":" 
        + ((curMin < 10) ? "0" : "") + curMin + ":" 
        + ((curSec < 10) ? "0" : "") + curSec 
        + curAMPM;
    return curTime;   
  }
}
//global variable for timer
var jbTimer = new JBTimerManager();

//private function

//reset timer

//timer off function, run the custom function and reset timer
function runTimer(ele)
{
  var timer = jbTimer.get(ele);
  eval(timer.funcName);
  timer.count++;
  if(timer.numberOfTimes && timer.count>timer.numberOfTimes)
  {
    timer.stop();
  }else
  {
    timer.reset();
  }
} 

//cancel a outstanding timer

//constructor for a timer, private, should use jbTimer.add to create a new timer
function Timer(timerId, func, value, numberOfTimes)
{
  this.started=false;
  this.timerHandler='';
  this.id=timerId;
  this.funcName=func;
  this.interval= value;
  this.numberOfTimes=numberOfTimes;
  
  this.start= function ()
  {
    if(this.started) return;
    this.started=true;
    this.reset();
  };

  this.stop=function ()
  {
    this.count=1;
    window.clearTimeout(this.timerHandler);
    this.timerHandler='';
    this.started=false;
  };
  
  this.reset=function()
  {
    this.timerHandler = window.setTimeout("runTimer('"+this.id+"');", this.interval);
  };
  
  this.count=1;
  this.date=function()
  {
    var d=new Date();
    var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    return weekday[d.getDay()] + " " + d.getDate() + ". " + monthname[d.getMonth()] + " "+ d.getFullYear();
  }
  this.time=function()
  {
    var time = new Date()
    var hours = time.getHours()
    var minutes = time.getMinutes()
    minutes=((minutes < 10) ? "0" : "") + minutes
    var seconds = time.getSeconds()
    seconds=((seconds < 10) ? "0" : "") + seconds
    var clock = hours + ":" + minutes + ":" + seconds
    return clock;
  }
}
